// JavaScript Document

function Live()
{
	var lwType = $(".lwSend span img").attr("src"); //礼物类型
	this.init();	
}

Live.prototype =
{
	init : function()
	{
		var _this=this;
		_this.payment();
		_this.liveInfo();
		_this.report();
		_this.autoScrollImg();
		_this.followUser();
		//_this.setAlList();
		_this.tab($(".phb"));
		_this.phbList();
		_this.liveChatAd();
		//_this.sendLw();
		_this.loading();
		_this.sendChat(); //回车发送消息
		//_this.sideScroll();
		_this.liveTj();
		_this.setLiveSize();
	},
	//王者荣耀直播
	liveTj : function()
	{
		$(".liveList").each(function()
		{
			var self = $(this);
			self.find("li").on("mouseover",function()
			{
				$(this).find(".livePlay").css({"transform":"scale(1)"}).stop().animate({"opacity":"0.9"});
				$(this).find(".liveMask").stop().animate({"opacity":"1"});
			});	
			
			self.find("li").on("mouseout",function()
			{
				$(this).find(".livePlay").stop().animate({"opacity":"0"}).css({"transform":"scale(1.8)"});
				$(this).find(".liveMask").stop().animate({"opacity":"0"});
			});	
		});
	},
	//广告
	liveChatAd:function()
	{
		$(".btnAdClose").bind("click",function()
		{
			$(".liveChatAd").hide();	
		});
	},
	//phbList滚动
	phbList:function()
	{
		$(".phbLw").each(function()
		{
			var self = $(this);	
			self.bind("mouseover",function()
			{
				$(this).find(".jobCont").stop().animate({"height":"305px"});
			});
			self.bind("mouseout",function()
			{
				$(this).find(".jobCont").stop().animate({"height":"0"});
			});	
		});	
	},
	//礼物榜 亲密榜 主播公告切换
	tab:function(obj)
	{
		var _this = this;
		var cTit  = obj.find("h3 a");
		var cBox  = obj.find(".cBox");
		
		cBox.eq(0).show().siblings("div").hide();
		
		cTit.bind("click",function()
		{
			var cIndex = $(this).index();
			cTit.eq(cIndex).addClass("curr").siblings("a").removeClass("curr");
			cBox.eq(cIndex).show().siblings("div").hide();
		})
	},
	setLiveSize:function()
	{
		var videoWidth = 0;
		$(window).bind("load resize",function()
		{
			if($(window).width()<=1300)
			{
				$(".liveWrap").css({'width':'1300px'});
				$(".navCont").css({'width':'1100px'});
				videoWidth = 786;
			}
			else
			{
				$(".liveWrap,.navCont").css({'width':'auto'});	
				videoWidth = (parseInt($(window).width())-514);
			}
			
			//video
			$(".liveVideo").css({"width":videoWidth+"px","height":parseInt(videoWidth*5/8.85)+"px"});
			$(".liveLeft").css({"height":(parseInt(videoWidth*5/8.85)+156)+"px"});
			$(".chatMsg").css({"height":(parseInt(videoWidth*5/8.85)-218)+"px"});
		});	
	},
	//关注
	followUser:function()
	{
		$(".btnFollowUser").bind("click",function()
		{
			if(!$(this).hasClass("btnFollowed"))
			{
				$(this).addClass("btnFollowed");
			}
			else
			{
				$(this).removeClass("btnFollowed");	
			}
			
		});
		//手机看直播
		$(".btnMobileLive").bind("mouseover",function()
		{
			$(".btnMobileLive span").show();
		});
		$(".btnMobileLive").bind("mouseout",function()
		{
			$(".btnMobileLive span").hide();
			
		});
		
	},
	//用户充值
	payment:function()
	{
		//显示弹框
		$(".liveSm .btnPay").on("click",function()
		{
			$(".popPayment").show();
		});
		
		//隐藏弹框
		$(".popPayment .close").on("click",function()
		{
			$(".popPayment").hide();
			$(".payList li").eq(0).addClass("curr").siblings("li").removeClass("curr");
			$(".payMoney em").html("6");
		});
		
		//选取充值数量
		$(".payList li").on("click",function()
		{
			$(this).addClass("curr").siblings("li").removeClass("curr");
			$(".payMoney em").html($(this).find("a .r").html().substring(1));
		});
		
		//选择支付方式
		$(".payClass a").on("click",function()
		{
			$(this).addClass("curr").siblings("a").removeClass("curr");
		});
	},
	//举报
	report:function()
	{
		//显示弹框
		$(".btnJb").on("click",function()
		{
			$(".popJb").show();
		});
		
		//隐藏弹框
		$(".popJb .close").on("click",function()
		{
			$(".popJb").hide();
		});
		
		//选择举报理由
		$(".popSelect a").on("click",function(e)
		{
			e.stopPropagation();
			$(".popSelect span").show();	
		});
		
		$(".popSelect span a").on("click",function()
		{
			$(".popSelect a i").html($(this).html());	
			$(".popSelect span").hide();
		});
		$(document).on("click",function()
		{
			$(".popSelect span").hide();	
		})
	},
	//主播简介
	liveInfo:function()
	{
		$(".liveMaBox").on("mouseover",function()
		{
			$(".liveMaText").show();
		});
		$(".liveMaBox").on("mouseout",function()
		{
			$(".liveMaText").hide();
		});
	},
	//选择礼物滚动
	autoScrollImg:function ()
	{
		var aList = $(".liveLwlist li");
		var initX = -56;
		var initY = -89;
		var oX    = 0;
		var oY    = 0;
		var rowCount = 0; //每行个数
		
		$(window).on("load resize",function()
		{
			if($(window).width()<=1340)
			{
				$(".liveLwWrap").css({'width':'438px'});
				rowCount = 7;
			}
			else
			{
				$(".liveLwWrap").css({'width':'612px'});
				rowCount = 10;
			}	
		});
		
		//滑动弹出框
		aList.on("mouseover",function()
		{
			var aA    = $(this).find("a");
			var name  = aA.attr("data-name");
			var money = aA.attr("data-money");
			var img   = aA.attr("data-img");
			count = $(this).index();
			$(".lwImage img").attr("src",img);
			$(".lwName").html(name);
			$(".lwMoney").html(money);

			oX = $(this).index()%rowCount*58+initX;
			oY = Math.floor($(this).index()/rowCount)*56+initY;
			$(".popLw").css({"left":oX + "px","top":oY + "px"}).show().stop().animate({"opacity":1});
			
		});
		
		aList.on("mouseout",function()
		{
			$(".popLw").stop().animate({"opacity":0}).hide();
		});
		
		aList.bind("click",function()
		{
			var img =$(this).find("a").attr("data-img");
			$(this).addClass("curr").siblings("li").removeClass("curr");
			$(".lwSend span img").attr("src",img);
			$(".lwSend span").attr({"data-type":$(this).find("a").attr("data-type"),"data-name":$(this).find("a").attr("data-name")});
		});	
	
		//展示更多礼物
		$(".btnShowHide").on("click",function()
		{
			var em  = $(this).find("em");
			if(!em.hasClass("jtRotateShow"))
			{
				em.addClass("jtRotateShow").removeClass("jtRotateHide");
				$(".liveLwlist").css({"height":"auto"});
			}
			else
			{
				em.addClass("jtRotateHide").removeClass("jtRotateShow");
				$(".liveLwlist").css({"height":"64px"});
			}
		})
		
		//弹出礼物数量
		$(".lwSend .iconJt").bind("click",function()
		{
			if(!$(this).hasClass("jtRotateShow"))
			{
				$(this).addClass("jtRotateShow").removeClass("jtRotateHide");
				$(".lwSendLx").show();
			}
			else
			{
				$(this).addClass("jtRotateHide").removeClass("jtRotateShow");
				$(".lwSendLx").hide();
			}
			
		});
		
		$(".lwSendLxct a").bind("click",function()
		{
			$(".iconJt").addClass("jtRotateHide").removeClass("jtRotateShow");
			$(".lwSendLx").hide();
			var num =$(this).attr("data-num");
			$(".lwSend .lwCurrCount").val(num);
		});
	},
	sendLw:function()
	{
		
		var lwCount      = $(".lwCurrCount").val(); //礼物数量
		var lwClickCount = 1;  //连击次数
		var lwObj        = ""; //礼物html
		var id           = 1;  //礼物ID
		var lwHitTime    = 2;
		var objArr       = [];
		var countDownEnd = false; //倒计时结束标识
		var countDown = 10; //倒计时时长
		var iBtnClick = false;
		
		$(".btnZs").on("click",function()
		{
			
			/*
				需要新创建礼物:
				a.第一次点击赠送礼物  
				b.连击结束后点击赠送礼物 
				c.礼物数量改变后点击赠送礼物
				d.礼物改变后点击赠送礼物
			*/
			
			if($(".liveLwList .liveSendWrap").length<=0 || countDownEnd==true || this.lwType != $(".lwSend span img").attr("src") || lwCount!=$(".lwCurrCount").val())
			{
				lwObj = '<div class="liveSendWrap" id="lw'+ id +'">'+
							'<div class="liveSendLw boxBbt">'+
								'<span class="liveHeader"><img src="misc/img/test5.jpg" /></span>'+
								'<h3 class="liveSendName"><em>雍容风雅</em><p>送出<i>'+$(".lwSend span").attr("data-name")+'</i><i>'+$(".lwCurrCount").val()+'个</i></p></h3>'+
								'<span class="iconLw"><img src="'+ $(".lwSend span img").attr("src") +'"></span>'+
								'<span class="liveSendCh"></span>'+
								'<span class="liveSendCount liveLwScale">'+ lwClickCount +'</span>'+
							'</div>'+
						'</div>';
				var currObj = {};
				currObj.lwTimer = null;
				$(".liveLwList").append(lwObj);
				objArr.push($("#lw"+id));				
				$("#lw"+id).animate({"margin-left":"0"},"fast");
				countDownEnd=false;
				id++;
				lwClickCount = 1;
				this.lwType = $(".lwSend span img").attr("src");
			}
			else
			{
				objArr[objArr.length-1].find(".liveSendCount,.liveSendCh").addClass('liveLwScale');
				setTimeout(function ()
				{   
					objArr[objArr.length-1].find(".liveSendCount,.liveSendCh").removeClass('liveLwScale');
				}, 110);
				objArr[objArr.length-1].find(".liveSendCount").html(lwClickCount);
			}
			lwClickCount++;
			
			//连击赠送礼物
			$(".btnZsText").hide();
			clearInterval(currObj.lwTimer);
			countDown=10;
			currObj.lwTimer = setInterval(function()
			{
				if(countDown<=0)
				{
					//礼物滑出
					var lwCancel=setTimeout(function()
					{
						
						$("#lw"+(id-1)).animate({"margin-left":"-380px"},"fast",function()
						{
							$("#lw"+(id-1)).remove();
							lwClickCount = 1;
						});
						/*
						for(var jj = 0;jj < id;jj++){
							$("#lw"+(jj-1)).animate({"margin-left":"-380px"},"fast",function()
							{
								$("#lw"+(jj-1)).remove();
								lwClickCount = 1;
								clearTimeout(lwCancel);
							});
						}*/
					},2000);
					
					//显示赠送
					$(".btnZsText").show();
					
					//倒计时结束初始化倒计时
					countDown=10;
					
					//倒计时结束标示
					countDownEnd=true;
					
					//停止倒计时
					clearInterval(currObj.lwTimer);
					
				}
				$(".btnZsLj i").html(countDown);
				countDown--;
			},100);
		});
	},
	setAlList:function()
	{
		$(window).bind("load resize",function()
		{
			var winWidth=$(window).width();
			
			if(winWidth<=1340)
			{
				//$(".hotTj .alList").css({"width":"1250px"});
				//$(".hotTj .alList li").css({"width":(1250/5-10)+"px"});
				$(".alList .alMask img,.alList .alGame img").css({"height":"158px"});
			}
			else
			{
				//$(".hotTj .alList").css({"width":(winWidth-90)+"px"});
				//$(".hotTj .alList li").css({"width":((winWidth-150)/6)+"px"});	
				$(".alList .alMask img,.alList .alGame img").css({"height":($(".hotTj .alList li a").get(0).offsetHeight)+"px"});
			}
			//$(".hotTj .alList").css({"height":($(".hotTj .alList li a").get(0).offsetHeight+70)+"px"});
		});
	},
	loading:function()
	{
		var str = "";
		var iNow = 0;
		setInterval(function()
		{
			iNow++;
			str+=".";
			if(iNow>=4)
			{
				str="";
				iNow=0;
			}
			$(".liveLoading span").html("加载中"+str);
		},400);	
	},
	sendChat:function()
	{
		$("body").keyup(function(event)
		{
			if(event.keyCode==13)
			{
				if($(".chatSendMsg").is(":focus"))
				{
					$(".btnSend").click();
				}
			}
		});	
		
		//获取焦点
		var obj = $(".chatSendMsg");
		
		obj.each(function()
		{
			var This=$(this), cValue=This.val();
			
			This.bind("focus",function()
			{
				if(This.val()==cValue)
				{
					This.val("");
				}
			});
			
			This.bind("blur",function()
			{
				if(This.val()=="")
				{
					This.val(cValue);
					$(".btnChatSend").removeClass("btnCtsd");
					This.css('color','#c8c8c8');
				}
			});
			
			This.bind("keyup",function()
			{
				$(".btnChatSend").addClass("btnCtsd");
				This.css('color','#000');
			});
		});
		

		$(".chatLogin").delegate(".btnCtsd","click",function()
		{
			alert($(".chatSendMsg").val());	
			$(".chatSendMsg").val("快和主播互动聊天吧").css('color','#c8c8c8');
		});
	}
};

$(function()
{
	new Live();
});
